const {sqlList, sqlTotal} = require("../sql/rows");
const handlerList = async (req, res, tableName) => {
    try {
        const {current, pageSize, data} = req.body

        const [total] = await req.db.query(sqlTotal(tableName))

        const sql = sqlList(tableName, {
            LIMIT: pageSize,
            OFFSET: (current - 1) * 10,
            data,
        })
        // console.log(sql)
        const rows = await req.db.query(sql)

        return res.status(200).send({
            code: 200,
            message: "",
            data: {
                current: current,
                pageSize: pageSize,
                list: rows.length ? rows.map(item => {
                    const obj = JSON.parse(JSON.stringify(item))
                    for (const key in item) {
                        if (key.includes('Id') || key.includes('_id')) {
                            delete obj.id
                        }
                    }
                    return obj
                }) : [],
                total: total["COUNT(*)"]
            },
        })
    } catch (e) {
        console.log(e)
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
}

module.exports = handlerList